//
//  User.swift
//  CHT
//
//  Created by aiquantong on 2019/2/20.
//  Copyright © 2019 difan. All rights reserved.
//

import UIKit

enum MUMBER_LEVEL{
    case FOREVER
    case HIGH
    case NORMARL
    case FREE
    case FORBID
}


class User: NSObject {
    private static var shareInstance : User?
    
    public var isLogin : Bool!
    public var account : String!
    public var token : String!
    public var password : String!
    public var nameCN : String!
    public var mobile : String!
    
    public var sex : String!
    public var ownType : String!
    public var postion : String!
    public var qq : String!
    public var mail : String!
    
    public var companyName : String!
    public var personType : String!
    public var product : String!
    public var companyPhone : String!
    public var companyFix : String!
    public var companyWWW : String!
    public var companyAddress : String!
    
    public var industry : String!
    public var companyIntro : String!
    public var cfdenglu : String!
    
    public var province : String!
    public var city : String!
    public var fileUploadId : String!
    
    public var beginDate : String!
    public var endDate : String!
    public var flag : String!
    public var balance : String!
    
    
    class func share() -> User? {
        if shareInstance == nil {
            if FileManager.default.fileExists(atPath: User.achivePath()){
                shareInstance = NSKeyedUnarchiver.unarchiveObject(withFile: User.achivePath()) as? User
            }else{
                shareInstance = User()
            }
        }
        return shareInstance;
    }
    
    override private init (){
        super.init();
        fileUploadId = "-1";
        isLogin = false;
    }
    
    required init?(coder aDecoder: NSCoder) {
        // super.init(coder:) is optional, see notes below
        account = aDecoder.decodeObject(forKey: "account") as? String
        password = aDecoder.decodeObject(forKey: "password") as? String
        if aDecoder.containsValue(forKey:"token") {
            token = aDecoder.decodeObject(forKey: "token") as? String
        }
        
        nameCN = aDecoder.decodeObject(forKey: "nameCN") as? String
        mobile = aDecoder.decodeObject(forKey: "mobile") as? String
        
        sex = aDecoder.decodeObject(forKey: "sex") as? String
        ownType = aDecoder.decodeObject(forKey: "ownType") as? String
        postion = aDecoder.decodeObject(forKey: "postion") as? String
        qq = aDecoder.decodeObject(forKey: "qq") as? String
        
        companyName = aDecoder.decodeObject(forKey: "companyName") as? String
        personType = aDecoder.decodeObject(forKey: "personType") as? String
        product = aDecoder.decodeObject(forKey: "product") as? String
        companyPhone = aDecoder.decodeObject(forKey: "companyPhone") as? String
        
        industry = aDecoder.decodeObject(forKey: "industry") as? String
        companyIntro = aDecoder.decodeObject(forKey: "companyIntro") as? String
        cfdenglu = aDecoder.decodeObject(forKey: "cfdenglu") as? String
        
        province = aDecoder.decodeObject(forKey: "province") as? String
        city = aDecoder.decodeObject(forKey: "city") as? String
        fileUploadId = aDecoder.decodeObject(forKey: "fileUploadId") as? String
        flag = aDecoder.decodeObject(forKey: "mumberStr") as? String
        
        isLogin = aDecoder.decodeObject(forKey: "isLogin") as? Bool
    }
    
    
    func encodeWithCoder(coder aCoder: NSCoder) {
        aCoder.encode(account, forKey: "account");
        aCoder.encode(password, forKey: "account");
        aCoder.encode(token, forKey: "token");
        
        aCoder.encode(nameCN, forKey: "nameCN");
        aCoder.encode(mobile, forKey: "mobile");
        
        aCoder.encode(sex, forKey: "sex");
        aCoder.encode(ownType, forKey: "ownType");
        aCoder.encode(postion, forKey: "postion");
        aCoder.encode(qq, forKey: "qq");
        
        aCoder.encode(companyName, forKey: "companyName");
        aCoder.encode(personType, forKey: "personType");
        aCoder.encode(product, forKey: "product");
        aCoder.encode(companyPhone, forKey: "companyPhone");
        
        aCoder.encode(industry, forKey: "industry");
        aCoder.encode(companyIntro, forKey: "companyIntro");
        aCoder.encode(cfdenglu, forKey: "cfdenglu");
        
        aCoder.encode(province, forKey: "province");
        aCoder.encode(city, forKey: "city");
        aCoder.encode(fileUploadId, forKey: "fileUploadId");
        aCoder.encode(flag, forKey: "mumberStr");
        
        aCoder.encode(isLogin, forKey: "isLogin");
    }
    
    
    private class func achivePath() -> String {
        var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let docPath: String = paths[0] as String
        let userPath = docPath + "/user.archiver"
        return userPath;
    }
    
    func achiveUserInfo() -> Void {
        NSKeyedArchiver .archiveRootObject(self, toFile:User.achivePath());
    }
    
    func isCompany() -> Bool {
        if (personType != nil && personType == "公司") {
            return true;
        }
        return false;
    }
    
    func login(_ responseDic : NSDictionary) -> Void {
        
        token = responseDic["token"] as? String;
        nameCN = responseDic["name"] as? String;
        mobile = responseDic["mobile"] as? String;
        sex = responseDic["sex"] as? String;
        ownType = responseDic["chlx"] as? String;
        
        qq = responseDic["qq"] as? String;
        companyName = responseDic["qymc"] as? String;
        personType = responseDic["gs"] as? String;
        product = responseDic["zycp"] as? String;
        companyPhone = responseDic["dh"] as? String;
        industry = responseDic["hangye"] as? String;
        
        companyIntro = responseDic["qyjj"] as? String;
        province = responseDic["sf"] as? String;
        city = responseDic["city"] as? String;
        flag = responseDic["flag"] as? String;
        
        fileUploadId = responseDic["userid"] as? String;
        
        if token.count > 5 && fileUploadId.count > 0 {
            isLogin = true;
            
            [JPUSHService.setTags([fileUploadId],
                                   completion: { (_ iResCode:Int, _ itags:Set<AnyHashable>?, _ seq:Int) in
            
            }, seq: 1000)];
            
            self.achiveUserInfo();
        }
    }
    
    func reloadUserInfoFromRemoteServer(_ personInfo:PersonInfoModel) -> Void {
        
    }
    
    func logout() -> Void {
        isLogin = false;
        
        if FileManager.default.fileExists(atPath: User.achivePath()){
            try? FileManager.default.removeItem(atPath: User.achivePath());
        }
        
        account = ""
        password = ""
        token = ""
        
        nameCN = ""
        mobile = ""
        
        sex = ""
        ownType = ""
        postion = ""
        qq = ""
        
        companyName = ""
        personType = ""
        product = ""
        companyPhone = ""
        
        industry = ""
        companyIntro = ""
        cfdenglu = ""
        
        province = ""
        city = ""
        fileUploadId = ""
        flag = ""
    }
    
    func getMumberLevel() -> MUMBER_LEVEL {
        
        return MUMBER_LEVEL.FREE;
    }
    
}

